package com.lishe.lishe.lishe.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.lishe.lishe.R;

/**
 * Bin
 */

public class GoodsNumDialog extends Dialog implements View.OnClickListener {

    private int mNum;
    private Context mContext;
    private EditText mEtCount;
    private SureClicker mSureClicker;
    private View mSub;

    public interface SureClicker {
        void onSureClick(int num);
    }

    public GoodsNumDialog(Context context, int num, SureClicker clicker) {
        super(context, R.style.SimpleDialog);
        this.mNum = num;
        this.mContext = context;
        this.mSureClicker = clicker;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_goodsnum);
        Window dialogWindow = getWindow();

        Display display = dialogWindow.getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        //设置点击Dialog外部任意区域关闭Dialog
        setCanceledOnTouchOutside(true);
        initView();
    }

    private void initView() {
        mSub = findViewById(R.id.sub);
        mSub.setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.sure).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);

        mEtCount = findViewById(R.id.count);
        mEtCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (s == null || "".equals(s.toString())) {
                    return;
                }

                if (Integer.parseInt(s.toString()) <= 1) {
                    mSub.setBackgroundResource(R.mipmap.ic_sub1);
                } else {
                    mSub.setBackgroundResource(R.mipmap.ic_sub);
                }
                mNum = Integer.parseInt(s.toString());
            }
        });
        mEtCount.setText(mNum + "");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sub:
                if (mNum <= 1) {
                    return;
                }
                mNum -= 1;
                mEtCount.setText(mNum + "");
                break;
            case R.id.add:
                mNum += 1;
                mEtCount.setText(mNum + "");
                break;
            case R.id.sure:
                if (mNum > 0) {
                    mSureClicker.onSureClick(mNum);
                }
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }
}
